#include "Graphics.h"

Graphics::Graphics(void)
{

}

Graphics::~Graphics(void)
{

}

void Graphics::Initialize(HWND WindowHandle, HINSTANCE hInstance, ID3D11Device* Device, ID3D11DeviceContext* DeviceContext)
{
	DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory( &sd, sizeof( sd ) );
    sd.BufferCount = 1;
    sd.BufferDesc.Width = 640;
    sd.BufferDesc.Height = 480;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = WindowHandle;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;

	D3D_FEATURE_LEVEL featureLevels[] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
    };
	UINT numFeatureLevels = ARRAYSIZE( featureLevels );

	D3D11CreateDeviceAndSwapChain(nullptr,D3D_DRIVER_TYPE_HARDWARE,nullptr, D3D11_CREATE_DEVICE_DEBUG, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &sd, &SwapChain, &D3DDevice, nullptr, &D3DDeviceContext);

	ID3D11Texture2D *BackBuffer = nullptr;
    SwapChain->GetBuffer(0, __uuidof( ID3D11Texture2D ), reinterpret_cast<void**>( &BackBuffer ) );
	D3DDevice->CreateRenderTargetView(BackBuffer, nullptr, &RenderTargetView );
    BackBuffer->Release();
	D3DDeviceContext->OMSetRenderTargets( 1, &RenderTargetView, nullptr );

	D3D11_VIEWPORT vp;
    vp.Width = (FLOAT)640;
    vp.Height = (FLOAT)480;
    vp.MinDepth = 0.0f;
    vp.MaxDepth = 1.0f;
    vp.TopLeftX = 0;
    vp.TopLeftY = 0;
    D3DDeviceContext->RSSetViewports( 1, &vp );

	spriteBatch.reset( new SpriteBatch(D3DDeviceContext));
    spriteFont.reset( new SpriteFont(D3DDevice, L"font.binaryfont"));
}

void Graphics::Update(int DeltaTime)
{

}

void Graphics::Shutdown()
{
	if(RenderTargetView)
	{
		RenderTargetView->Release();
		RenderTargetView = 0;
	}
	if(SwapChain)
	{
		SwapChain->Release();
		SwapChain = 0;
	}
	if(D3DDeviceContext)
	{
		D3DDeviceContext -> Release();
		D3DDeviceContext = 0;
	}
	if(D3DDevice)
	{
		D3DDevice->Release();
		D3DDevice = 0;
	}
}

void Graphics::Render()
{
	D3DDeviceContext->ClearRenderTargetView(RenderTargetView, Colors::MidnightBlue );
}

void Graphics::RenderText(std::string Text, float x, float y)
{
	std::wstring widestr = std::wstring(Text.begin(), Text.end());
	const wchar_t* widecstr = widestr.c_str();
	spriteBatch->Begin();
	spriteFont->DrawString(spriteBatch.get(), widecstr, XMFLOAT2(x, y), Colors::White);
    spriteBatch->End();
}

void Graphics::Present()
{
	SwapChain->Present( 0, 0 );
}

ID3D11Device* Graphics::GetDevice()
{
	return D3DDevice;
}

ID3D11DeviceContext* Graphics::GetDeviceContext()
{
	return D3DDeviceContext;
}

ID3D11RenderTargetView* Graphics::GetRenderTarget()
{
	return RenderTargetView;
}

IDXGISwapChain* Graphics::GetSwapChain()
{
	return SwapChain;
}

